// 操作User表的路由
const express = require('express');
// 引入连接池模块
const pool = require('../pool');
// 引入路由器
const router = express.Router();

// 添加用户登录接口
router.post('/login', (req, res, next) => {
	
	console.log(req.body)
	let { phone, password } = req.body;

	let sql = 'select * from user_accounts where phone=? and password=?';
	pool.query(sql, [phone, password], (err, r) => {
		// 如果有错误放回给错误处理中间件
		if (err) {
			return next(err);
		} 
		if(r.length==0){
			res.send({status:400,msg:'账号密码错误', data: r});
		}else {
			// 返回的数据
			res.send({ status: 200, msg: '登陆成功', data: r });
		}
	});
});
// 添加用户注册接口
router.post('/regist', (req, res, next) => {
	let { phone, password, email } = req.body;

	let sql = 'insert into user_accounts (phone, email, password) values (?,?,?)';
	pool.query(sql, [phone, email, password], (err, r) => {
		// 如果有错误放回给错误处理中间件
		if (err) {
			return next(err);
		} else {
			// 返回的数据
			res.send({ status: 200, msg: '注册成功', data: r });
		}
	});
});

module.exports = router;
